﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Collections;

namespace CoolERP_Web.User
{
    [PermissionCheck(false)]
    public partial class Default : BasePage
    {
        private List<Menus> itemLists = new List<Menus>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        private void BindData()
        {
            using (MenusBLL mBLL = new MenusBLL())
            {
                if (Utility.IsCheckPermission())
                    itemLists = mBLL.GetList(t => t.Enabled == 1 && this.currentUser.PermissionCodes.Contains(t.Code), "Sort desc");
                else
                    itemLists = mBLL.GetList(t => t.Enabled == 1, "Sort desc");
                Repeater1.DataSource = itemLists.Where(t => t.ParentId == null);
                Repeater1.DataBind();

                #region======用以缓存权限相关的数据======
                CacheManager cache = new CacheManager();
                if (!cache.Exist(Utility.UrlCacheKey))
                {
                    //缓存User下的页面（url/code）
                    Hashtable ht = new Hashtable();
                    foreach (var item in mBLL.GetAll())
                    {
                        if (!string.IsNullOrEmpty(item.Url))
                            ht[item.Url] = item.Code;
                    }
                    cache.Add(Utility.UrlCacheKey, ht);
                }
                #endregion
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.DataItem != null)
            {
                Repeater rep = e.Item.FindControl("repChild") as Repeater;
                Menus item = e.Item.DataItem as Menus;
                rep.DataSource = itemLists.Where(t => t.ParentId == item.Id);
                rep.DataBind();
            }
        }

        protected void lnkbExit_Click(object sender, EventArgs e)
        {
            Session.Remove(this.SessionKey);
            CacheManager cache = new CacheManager();
            cache.Clear();
            Response.Redirect(this.LoginUrl);
        }
    }
}